var express = require('express');
var router = express.Router();
const admin = require("../sql/admin");
const sql = require("../sql/sql");
const md5 = require("md5")

router.get('/', function(req, res, next) {
  res.render('login');
});

router.get('/action', function(req, res, next) {
  const data = req.query;

  sql.get(admin, {username: data.username, password: md5(data.password)}).then(data=>{
    if(data.length < 1){
      res.redirect("/login");
    }else{
      // 利用cookie进行状态存储
      // res.cookie("isLogin","ok");

      // 利用session进行状态存储
      req.session.isLogin = "ok";
      req.session.power = data[0].power;

      res.redirect("/");
    }
  })
});

module.exports = router;
